package com.xsherl.leetcode.solution;

import com.xsherl.leetcode.utils.PrintUtils;

public class SortColors {

    /**
     * 计数排序
     */
    public void sortColors(int[] nums) {
        int redCount = 0, whiteCount = 0;
        for (int n: nums){
            if (n == 0){
                redCount++;
            } else if (n == 1){
                whiteCount++;
            }
        }
        whiteCount += redCount;
        for (int i = 0; i < nums.length; ++i){
            if (i < redCount){
                nums[i] = 0;
            } else if (i < whiteCount){
                nums[i] = 1;
            } else {
                nums[i] = 2;
            }
        }
    }

    public static void main(String[] args) {
        int[] nums = {2,0,2,1,1,0};
        new SortColors().sortColors(nums);
        PrintUtils.println(nums);
    }
}
